package com.easylinkin.linkappapi.grid.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.grid.entity.GridAreaImg;
import com.easylinkin.linkappapi.grid.mapper.GridAreaImgMapper;
import com.easylinkin.linkappapi.grid.models.dto.GridAreaImgDTO;
import com.easylinkin.linkappapi.grid.service.IGridAreaImgService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.time.LocalDateTime;

/**
 * 网格分区图 服务实现类
 *
 * @author
 * @since 2024-05-15
 */
@Service
public class GridAreaImgServiceImpl extends ServiceImpl<GridAreaImgMapper, GridAreaImg> implements IGridAreaImgService {

    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    /**
     * 更新/保存网格分区图
     *
     * @param gridAreaImgDTO:
     * @return site.morn.rest.RestMessage
     * @author Jereay
     * @date 2024/5/15 13:44
     */
    @Override
    public RestMessage saveGrifImg(GridAreaImgDTO gridAreaImgDTO) {
        // 获取当前登录用户
        LinkappUser user = linkappUserContextProducer.getNotNullCurrent();

        GridAreaImg gridAreaImg = this.getOne(Wrappers.<GridAreaImg>lambdaQuery()
                .eq(GridAreaImg::getTenantId, user.getTenantId())
                .last(" limit 1")
        );
        if (ObjectUtil.isNotNull(gridAreaImg)) {
            GridAreaImg areaImg = new GridAreaImg();
            areaImg.setId(gridAreaImg.getId());
            areaImg.setImgUrl(gridAreaImgDTO.getImgUrl());
            areaImg.setModifyTime(LocalDateTime.now());
            areaImg.setTenantId(user.getTenantId());
            areaImg.setModifyId(user.getId().toString());
            areaImg.setCreateTime(LocalDateTime.now());
            boolean b = this.updateById(areaImg);
            if(b){
                return RestBuilders.successBuilder().message("保存成功").build();
            }
        } else {
            GridAreaImg areaImg = new GridAreaImg();
            areaImg.setImgUrl(gridAreaImgDTO.getImgUrl());
            areaImg.setModifyTime(LocalDateTime.now());
            areaImg.setModifyId(user.getId().toString());
            boolean b = this.save(areaImg);
            if(b){
                return RestBuilders.successBuilder().message("保存成功").build();
            }
        }
        return RestBuilders.errorBuilder().message("保存失败").build();
    }

    @Override
    public RestMessage queryGrifImg() {
        // 获取当前登录用户
        LinkappUser user = linkappUserContextProducer.getNotNullCurrent();
        GridAreaImg gridAreaImg = this.getOne(Wrappers.<GridAreaImg>lambdaQuery()
                .eq(GridAreaImg::getTenantId, user.getTenantId())
                .last(" limit 1")
        );
        if (ObjectUtil.isNotNull(gridAreaImg)) {
            return RestBuilders.successBuilder().data(gridAreaImg.getImgUrl()).build();
        }
        return RestBuilders.successBuilder().build();
    }
}